import { findStateListByType, userIsBlack } from '@/api/box/api'
import { findLogisticsCompany, findBoxSupplyReason, findBoxSupplyStatus } from '@/api/box/order'
import { attrList, attrValueList } from '@/api/box/setting'
import { reject } from 'lodash'
import html2canvas from "html2canvas"
const globalData = {}
export default globalData

// 服务单状态
export function getOrderStatus () {
  let params = {
    requestData: {
      type: 0
    }
  }
  return findStateListByType(params).then(res => {
    if (res.code == 0) {
      globalData.orderStatus = res.data
      return res.data
    } else {
      this.$message.warning(res.msg)
      return []
    }
  }).catch(err => {
    console.log(err)
  })
}
// 商品状态
export function getGoodsStatus () {
  let params = {
    requestData: {
      type: 1
    }
  }
  return findStateListByType(params).then(res => {
    if (res.code == 0) {
      globalData.goodsStatus = res.data
      return res.data
    } else {
      this.$message.warning(res.msg)
      return []
    }
  }).catch(err => {
    console.log(err)
  })
}
// 服务单操作
export function getOrderAction () {
  let list = [
    {
      name: '物流',
      action: [1, 2, 9, 10, 3, 5]
    },
    {
      name: '删款',
      action: [0]
    },
    {
      name: '补货',
      action: [1, 2]
    },
    {
      name: '还货',
      action: [2, 9]
    },
    {
      name: '异常处理',
      action: [3, 10]
    },
    {
      name: '重制',
      action: [6, 7, 8]
    },
    {
      name: '取消',
      action: [0]
    },
    {
      name: '测试发货',
      action: [0]
    },
    {
      name: '测试签收',
      action: [1]
    },
    {
      name: '测试完成',
      action: [2, 9, 10, 3]
    },
    {
      name: '还货记录',
      action: [3, 5, 10]
    },
    {
      name: '结算',
      action: [1, 2, 9]
    }
  ]
  return list
}
// 快递公司
export function getLogisticsCompany () {
  return findLogisticsCompany().then(res => {
    if (res.code == 0) {
      globalData.logisticsCompany = res.data
      return res.data
    } else {
      this.$message.warning(res.msg)
      return []
    }
  }).catch(err => {
    console.log(err)
  })
}
// 补货理由
export function getSupplyReason () {
  return findBoxSupplyReason().then(res => {
    if (res.code == 0) {
      globalData.boxSupplyReason = res.data
      return res.data
    } else {
      this.$message.warning(res.msg)
      return []
    }
  }).catch(err => {
    console.log(err)
  })
}
// 补货单状态
export function getSupplyStatus () {
  return findBoxSupplyStatus().then(res => {
    if (res.code == 0) {
      globalData.boxSupplyStatus = res.data
      return res.data
    } else {
      this.$message.warning(res.msg)
      return []
    }
  }).catch(err => {
    console.log(err)
  })
}

/** 下载文件自定义文件名 */
export function onDownFile (url, filename) {
  getBlob(url, (blob) => {
    saveAs(blob, filename)
  })
}

export function getBlob (url, cb) {
  var xhr = new XMLHttpRequest()
  xhr.open('GET', url, true)
  xhr.responseType = 'blob'
  xhr.onload = function () {
    if (xhr.status === 200) {
      cb(xhr.response)
    }
  }
  xhr.send()
}

export function saveAs (blob, filename) {
  if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob, filename)
  }
  else {
    var link = document.createElement('a')
    var body = document.querySelector('body')

    link.href = window.URL.createObjectURL(blob)
    link.download = filename

    link.style.display = 'none'
    body.appendChild(link)

    link.click()
    body.removeChild(link)
    window.URL.revokeObjectURL(link.href)
  }
}

/** 时间戳 */
export function timeStampDate (timeStamp) {
  if (timeStamp) {
    var time
    if (parseInt(timeStamp).length == 10) {
      // 时间戳为10位需*1000,13位则无需
      time = new Date(parseInt(timeStamp) * 1000)
    } else {
      time = new Date(parseInt(timeStamp))
    }

    var Y = time.getFullYear() + '-'
    var M = (time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1) + '-'
    var D = (time.getDate() < 10 ? '0' + time.getDate() : time.getDate())
    return Y + M + D
  }
}
export function timeStamp (timeStamp) {
  if (timeStamp) {
    var time
    if (parseInt(timeStamp).length == 10) {
      // 时间戳为10位需*1000,13位则无需
      time = new Date(parseInt(timeStamp) * 1000)
    } else {
      time = new Date(parseInt(timeStamp))
    }

    var Y = time.getFullYear() + '-'
    var M = (time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1) + '-'
    var D = (time.getDate() < 10 ? '0' + time.getDate() : time.getDate()) + ' '
    var h = (time.getHours() < 10 ? '0' + time.getHours() : time.getHours()) + ':'
    var m = (time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes()) + ':'
    var s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds()
    return Y + M + D + h + m + s
  }
}

/* 获取box内系统设置的数据字典 */
export function initDictFromBox (attrCode) {
  return new Promise((reslove, reject) => {
    let params = {
      page: { pageNo: 1, pageSize: 10 },
      requestData: { attrCode: attrCode }
    }
    attrList(params).then(res => {
      if (res.code == 0) {
        let result = res.data[0]
        dictDetailList(result.id).then(res => {
          reslove(res)
        }).catch()
      } else {
        this.$message.warning(res.msg)
        reject()
      }
    })
  })
}
export function dictDetailList (id) {
  return new Promise((reslove, reject) => {
    let params = {
      page: { pageNo: 1, pageSize: 50 },
      requestData: { attrId: id }
    }
    attrValueList(params).then(res => {
      if (res.code == 0) {
        reslove(res.data)
      } else {
        this.$message.warning(res.msg)
        reject()
      }
    })
  })
}
export function getHtml2canvas (canvasID, defaultScale) {
  let scale = 1
  if (defaultScale) scale = defaultScale
  // console.log(canvasID)
  return html2canvas(canvasID, {
    backgroundColor: "transparent",
    scale: scale,
    allowTaint: false,
    useCORS: true
  }).then((canvas) => {
    let dataURL = canvas.toDataURL("image/png")
    // console.log(dataURL)
    return dataURL
  }).catch()
}

/*
 * 下载图片
 * e.g. downloadImage(imgUrl, '推广小程序海报')
 */
export function downloadImage (imgsrc, name) {
  var image = new Image()
  image.setAttribute('crossOrigin', 'anonymous')
  image.onload = function () {
    var canvas = document.createElement('canvas')
    canvas.width = image.width
    canvas.height = image.height
    var context = canvas.getContext('2d')
    context.drawImage(image, 0, 0, image.width, image.height)
    var url = canvas.toDataURL('image/jpeg')
    var a = document.createElement('a')
    var event = new MouseEvent('click')
    a.download = name || 'photo'
    a.href = url
    a.dispatchEvent(event)
  }
  image.src = imgsrc
}

/* 
 * 校验会员是否黑名单
 */
export function fetchUserIsBlack (unionId) {
  let params = {
    requestData: unionId
  }
  return userIsBlack(params).then(res => {
    if (res.code == 0) {
      return res.data
    } else {
      this.$message.warning(res.msg)
      return false
    }
  })
}